package com.module.admin.controller;

import com.module.core.shiro.ShiroKit;
import com.module.core.shiro.ShiroUser;
import com.module.user.domain.BaseResp;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class LoginController {



    @RequestMapping("/login")
    public String loginpage(){
        return "/login.btl";
    }

    @RequestMapping("/")
    public String index(Model model){
        model.addAttribute("error","") ;
        return "/index.btl";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "/back/welcome.btl";
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public BaseResp<String> login(String username, String password){
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject currentUser = SecurityUtils.getSubject();
        BaseResp resp = new BaseResp();

       try {
           currentUser.login(token);
           resp.setStatus(200);
           resp.setMessage("登录成功");
       }catch (AuthenticationException e){
           resp.setStatus(500);
           resp.setMessage(e.getMessage());
       }

        return resp;
    }

    @RequestMapping("/logout")
    public String logout(){
        ShiroKit.getSubject().logout();
        return "redirect:/login.btl";
    }

}
